import json
import random
import time

import requests
requests.packages.urllib3.disable_warnings()

# 获取签到状态
def getSignStatus(isReceive=0):
    url = 'https://tcmobileapi.17usoft.com/platformsign/sign/signIndex'
    data = {
        "isReceive": isReceive,
        "memberId": memberId,
        "platId": 100,
        "reqFrom": "app",
        "regid": "",
        "deviceId": deviceId,
        "token": requestToken,
    }
    data = baseRequest(url, headers, data)
    if data['statusCode'] == 0:
        data = data['data']
        # 当前里程
        mileageBalance = data['mileageBalance']
        # 今日签到信息
        signInfo = data['signInfo']
        # 今日签到可以获得多少里程
        todayMileage = signInfo['todayMileage']
        # 明天可以获得多少
        tommorrowMileage = signInfo['tommorrowMileage']
        # 连续天数
        continuedDays = signInfo['continuedDays']
        # 今天是否签到
        isSignResult = signInfo['isSignResult']
        signStatus = '已签到' if isSignResult else '未签到'
        print(f'当前账户里程为：{mileageBalance}，签到状态：{signStatus}，今日签到可获得：{todayMileage}，明天可以获得：{tommorrowMileage}，当前连续签到天数：{continuedDays}')
        if isSignResult is False:
            print('开始签到')
            getSignStatus(1)
    else:
        print(data)

# 获取任务列表
def getTaskList():
    url = 'https://tcmobileapi.17usoft.com/platformsign/task/queryTaskList'
    data = {
        "taskType": 1002,
        "memberId": memberId,
        "platId": 100,
        "deviceId": deviceId,
        "reqFrom": "app",
        "limit": 5
    }
    data = baseRequest(url, headers, data)
    taskStatusArray = {
        0: '待完成',
        1: '待领奖',
        2: '已完成'
    }
    if data['statusCode'] == 0:
        taskList = data['data']['taskModelList']
        for task in taskList:
            taskId = task['id']
            taskTitle = task['title']
            taskDesc = task['description']
            # status = 2 是已完成 0 是待完成 1 是待领奖
            taskStatus = task['status']
            print(f'任务ID：{taskId}，任务标题：【{taskTitle}】，任务描述：【{taskDesc}】，任务状态：{taskStatusArray[taskStatus]}')
            if taskStatus == 1:
                receiveTask(taskId)
            elif taskStatus == 0:
                randTime = random.randint(10, 13)
                print('正在完成任务ID：', taskId, '等待时间', randTime)
                time.sleep(randTime)
                commitTask(taskId)
                randTime = random.randint(1, 2)
                print('正在领取任务ID：', taskId, '等待时间', randTime)
                time.sleep(randTime)
                receiveTask(taskId)
            elif taskStatus == 2:
                print('任务已完成', taskId)


# 领取奖励
def receiveTask(taskId):
    url = 'https://tcmobileapi.17usoft.com/platformsign/task/receive'
    data = {
        "taskId": taskId,
        "memberId": memberId,
        "reqFrom": "app",
        "token": requestToken,
        "platId": 100,
        "deviceId": deviceId
    }
    data = baseRequest(url, headers, data)
    print('领取结果', data)

# 提交任务
def commitTask(taskId):
    url = 'https://tcmobileapi.17usoft.com/platformsign/task/commit'
    data = {
        "taskId": taskId,
        "memberId": memberId,
        "reqFrom": "app",
        "platId": 100
    }
    data = baseRequest(url, headers, data)
    print('提交结果', data)

# 商城抽奖
def shopLottery():
    url = 'https://wx.17u.cn/wcrewardshopapiv2/roulette/lottery'
    data = {
        "unionId": "unionId",
        "access_token": "undefined",
        "secToken": "undefined",
        "osType": 1,
        "onceFlag": True,
        "hostFakeUid": "",
        "playId": "IXuH79YDGkYNShOZEHy69g==",
        "nickName": "尊敬的会员",
        "taskNo": ""
    }
    headers['Referer'] = 'https://wx.17u.cn/memberlc/mileageshop/luckyWheel?refid=1486690823'
    headers['userKey'] = memberId
    headers['Host'] = 'wx.17u.cn'
    headers['platformSub'] = 'APP'
    headers['Origin'] = 'https://wx.17u.cn'
    headers['accountSystem'] = '2'
    headers['osType'] = '1'
    # headers['Cookie'] = 'flightRefId=5866741; traceid=0b2de938-790c-45b3-8bd7-1ca356534453; __tctma=217272534.1688441815749155.1688441815983.1697102604705.1697104755332.9; saviorInfo={"platid":"10114","chid":"tciphone","tc_deviceid":"B2B59E7A-4E24-4683-8785-CA96FEF73061","ecrd":"","v":"10.6.8","el_deviceid":"B2B59E7A-4E24-4683-8785-CA96FEF73061","refid":"5866741","memberid":"I0_9a4e7d6a0f91bfa000851f24a34bc581"}; ecid=c0065d121d76b2d9006d6944bc7cc514246dd6d1ce3ccad052468268c009d558603a321ff5a668dc63a16bb7f8c20e3e2089b6a596d27051ff46af3184070936; AS_100=c0065d121d76b2d9006d6944bc7cc514246dd6d1ce3ccad052468268c009d558603a321ff5a668dc63a16bb7f8c20e3e2089b6a596d27051ff46af3184070936; AS_101=c0065d121d76b2d9006d6944bc7cc514246dd6d1ce3ccad052468268c009d558603a321ff5a668dc63a16bb7f8c20e3e2089b6a596d27051ff46af3184070936'
    headers['UserToken'] = requestToken

    data = baseRequest(url, headers, data)
    if data['hasSuccess']:
        print('抽奖结果：', data['data']['prizeName'])
    else:
        print('抽奖结果：', data['resultInfo'])

# 发起请求
def baseRequest(url , headers , data):
    result = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
    content = result.content.decode('utf8')
    data = json.loads(content)
    return data

if __name__ == '__main__':
    headers = {
        'Host': 'tcmobileapi.17usoft.com',
        'Accept': 'application/json, text/plain, */*',
        'Sec-Fetch-Site': 'cross-site',
        'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Sec-Fetch-Mode': 'cors',
        'Content-Type': 'application/json;charset=utf-8',
        'Origin': 'https://appnew.ly.com',
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 TcTravel/10.6.8 tctype/wk',
        'Referer': 'https://appnew.ly.com/',
        'Sec-Fetch-Dest': 'empty',
        'Connection': 'keep-alive'
    }
    memberId = '2f1d12f88347922133a4f0f47d07f6d6'
    deviceId = 'B2B59E7A-4E24-4683-8785-CA96FEF73061'
    requestToken = 'meH7jFHafKlNRJLwoxYjrZm2Lrn43ZYIKKzQWyOY7DHVfdk_iJcm53Hy7SlB-jAq00R-4-nNVoXH3Xwf5zjwV_OTP6EViFtUslxTG7LB3FmPPWb1SoP7k1xHbdRPP-qDSk9NvGHqYzIgFFXh6fkbWRISt3FUKLnFcKQA_u3VlU_FI6R6UQTJRqFroqiEjlUAAISpT_ZbaKsPIIUeX1erm6bMxemFtNs9BjLdhkbeMyo6oiezo4hh5YTEuGVOVkZi-Oh97uqMBi-WRoTbsXhZ0SbWjW_YlMP6igg5HRnQdZ4*AB'
    # 先签到，再来完成任务
    print('-----------------开始签到流程-----------------')
    getSignStatus(0)
    # 完成任务
    print('-----------------开始任务流程-----------------')
    getTaskList()
    # 商城抽奖
    print('-----------------开始抽奖流程-----------------')
    shopLottery()